import { _decorator, Button, Color, Graphics, Label, Node, Sprite, SpriteAtlas, Vec2 } from 'cc';
import { UIComponent } from '../../../Framework/Scripts/Managers/UIComponent';
import { DW_Cfg, DWTYPE } from '../Config/GameCfg';
import { ResManager } from '../../../Framework/Scripts/Managers/ResManager';
import { AbNames, EventMsgKes } from '../Config/ResConfig';
import { Platform } from '../../../Framework/Scripts/Platform/PlatformWX';
import { SceneMgr } from '../../../Framework/Scripts/Managers/SceneMgr';
import { GetUserInfoRes, IChallengeGame, IDWItem, IThemeGame, IUserInfo } from '../../AssetPackage/Props/Hall/GetUserInfo';
const { ccclass, property } = _decorator;

/**
 * 用户信息
 */
@ccclass('GameUserInfo_Ctr')
export class GameUserInfo_Ctr extends UIComponent {
    graphics: Graphics;
    spHead: Sprite;
    labName: Label;
    item1: Node;
    item2: Node;
    item3: Node;
    lab1: Label; //挑战场次
    lab2: Label; //胜场   
    lab3: Label; //胜率
    labL: Label; //休闲挑战次数
    labR: Label; //大神挑战次数
    labRank: Label;//全国排名
    pos1: any[];
    pos2: any[];
    pos3: any[];
    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
        this.setBgAni(0);
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();

        this.spHead = this.ViewComponent('spUser/head/spHead',Sprite);
        this.labName = this.ViewComponent('spUser/labName',Label);
        this.item1 = this.ViewNode('ScrollView/view/content/item1');
        this.lab1 = this.ViewComponent('ScrollView/view/content/item1/spCC1/GamePlayerInfo_numBg/labCC',Label);
        this.lab2 = this.ViewComponent('ScrollView/view/content/item1/spCC2/GamePlayerInfo_numBg/labCC',Label);
        this.lab3 = this.ViewComponent('ScrollView/view/content/item1/spCC3/GamePlayerInfo_numBg/labCC',Label);

        this.labL = this.ViewComponent('ScrollView/view/content/item2/sp1/Label',Label);
        this.labR = this.ViewComponent('ScrollView/view/content/item2/sp2/Label',Label);
        this.labRank = this.ViewComponent('/ScrollView/view/content/item2/spCC1/GamePlayerInfo_numBg/labCC',Label);

        this.graphics = this.ViewComponent('ScrollView/view/content/item3/Node',Graphics);
        this.AddButtonListener('btnBack',this,this.OnBtnCloseClick1);
        this.AddButtonListener('spUser/btnShare',this,this.onBtnShare)
    }

    private InitData(){
        //划图形测试
        //三角形的边是330，
        this.pos1 = [];  //左 足球
        this.pos2 = [];  //上 文体
        this.pos3 = [];  //右 名族
     
        let bg = this.ViewNode('ScrollView/view/content/item3/bg');
        for (let i = 0; i < bg.children.length; i++) {
            let item = bg.children[i];
            if(i == 0){
                this.pos1.push(item.position);
                this.pos2.push(item.position);
                this.pos3.push(item.position);
            }else if(i > 0 && i < 6){
                this.pos1.push(item.position);
            }else if(i >= 6 && i < 11){
                this.pos2.push(item.position);
            }else{
                this.pos3.push(item.position);
            }
        }
        //测试
        // this.drawTriangle( this.pos1[2], this.pos2[3], this.pos3[4]);

        globalThis.gameMgr._GameHallMsg.GetUserInfoReq();
    }
    
    private registerEvent(){
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgGetUserInfoRes,this.onUsetInfoData,this);
    }

    private unRegisterEvent(){
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgGetUserInfoRes,this.onUsetInfoData,this);
    }
    
    private OnBtnCloseClick1(){
        SceneMgr.inst.showHall();
    }
    private onBtnShare(btn:Button){
        Platform.inst.shareFriend()
    }

    /**
     * 
     * @param v1 左边
     * @param v2 右边
     * @param v3 上面
     */
    private drawTriangle(v1: Vec2, v2: Vec2, v3: Vec2) {
        this.graphics.lineWidth = 6;
        this.graphics.strokeColor = new Color().fromHEX('68D8FB');
        this.graphics.fillColor =  new Color().fromHEX('53C8ED80');

        this.graphics.moveTo(v1.x, v1.y);
        this.graphics.lineTo(v2.x, v2.y);
        this.graphics.lineTo(v3.x, v3.y);
        this.graphics.close();
        this.graphics.fill();
        this.graphics.stroke();
    }

    private setItemInfo(itemName:string,data:IDWItem,idx: number){
        let atals = ResManager.inst.getAsset(AbNames.GameHall,'Atals/GameHall',SpriteAtlas) as SpriteAtlas; 
        let item = this.item1.getChildByName(itemName);
        let lab = item.getChildByName('Label');
        lab.getComponent(Label).string = DW_Cfg[idx];

        let labRank = item.getChildByName('spPM').getChildByName('labPM');
        if(this.hasProInObj(data,'dwRank')){
            labRank.getComponent(Label).string =   `全国排名：${data.dwRank}`;
        }else{
            labRank.getComponent(Label).string = '未上榜';
        }

        if(this.hasProInObj(data,'dwType')){
            let sf = atals.getSpriteFrame(`GamePlayerInfo${idx}_${data.dwType}`);
            if(sf){
                item.getChildByName('spIcon').getComponent(Sprite).spriteFrame = sf;
            }
        }
    }

    private onUsetInfoData(data: GetUserInfoRes) {
        console.log('======onUsetInfoData==========', data);
        if (!data) {
            return;
        }

        //用户数据
        if (this.hasProInObj(data, 'userInfo')) {
            let user: IUserInfo = data.userInfo;
            if (this.hasProInObj(user, 'avator')) {
                this.LoadRemoteImage(user.avator, this.spHead);
            }
            if (this.hasProInObj(user, 'name')) {
                this.labName.string = user.name;
            }
        }
        // 主题赛数据
        if (this.hasProInObj(data, 'themeGame')) {
            let themeGame: IThemeGame = data.themeGame;
            if (this.hasProInObj(themeGame, 'dwWTInfo')) {
                this.setItemInfo('sp1', themeGame.dwWTInfo, DWTYPE.DW_WENTI);
            }
            if (this.hasProInObj(themeGame, 'dwZqInfo')) {
                this.setItemInfo('sp2', themeGame.dwZqInfo, DWTYPE.DW_ZUQIU);
            }
            if (this.hasProInObj(themeGame, 'dwMzInfo')) {
                this.setItemInfo('sp3', themeGame.dwMzInfo, DWTYPE.DW_ZUQIU);
            }

            let num = '0';
            if (this.hasProInObj(themeGame, 'countTz')) {
                num = themeGame.countTz;
            }
            this.lab1.string = `${num}场`;
            num = '0';
            if (this.hasProInObj(themeGame, 'countTzWin')) {
                num = themeGame.countTzWin;
            }
            this.lab2.string = `${num}场`;

            if (this.hasProInObj(themeGame, 'countTz') && parseInt(themeGame.countTz) > 0 && this.hasProInObj(themeGame, 'countTzWin') && parseInt(themeGame.countTzWin) > 0) {
                this.lab3.string = ((parseInt(themeGame.countTzWin) / parseInt(themeGame.countTz)) * 100).toFixed(0) + '%';
            } else {
                this.lab3.string = '0%';
            }

            if (this.hasProInObj(themeGame, 'ranks')) {
                let v1 = this.pos2[parseInt(themeGame.ranks[0])];
                let v2 = this.pos1[parseInt(themeGame.ranks[1])];
                let v3 = this.pos3[parseInt(themeGame.ranks[2])];
                this.drawTriangle(v1, v2, v3);
            }
        }

        //每日挑战的数据
        if (this.hasProInObj(data, 'challengeGame')) {
            let challengeGame: IChallengeGame = data.challengeGame;
            if (this.hasProInObj(challengeGame, 'challengeXX')) {
                this.labL.string = `x${challengeGame.challengeXX}`;
            }
            if (this.hasProInObj(challengeGame, 'challengeDS')) {
                this.labR.string = `x${challengeGame.challengeDS}`;
            }
            if (this.hasProInObj(challengeGame, 'rankTz')) {
                this.labRank.string = challengeGame.rankTz.toString();
            }
        }
    }
}


